package org.distributed.transaction.core.connection;

import java.sql.Connection;

import org.distributed.transaction.core.annotion.TransactionType;
import org.distributed.transaction.core.callback.ConnectionCallback;
import org.distributed.transaction.core.context.TransactionContext;
import org.springframework.stereotype.Component;

@TransactionType
@Component
public class DefaultTransactionConnectionProxy implements TransactionConnectionProxy{

	@Override
	public Connection getConnection(ConnectionCallback callback) throws Throwable {
		/**
		 * 获取事务上下文
		 */
		TransactionContext transactionContext = TransactionContext.get();
		/**
		 * 事务组id
		 */
		String groupId = transactionContext.getGroupId();
		Connection connection = transactionContext.getConnection(groupId);
		if (connection != null) {
			return connection;
		}
		connection = new ProxyConnection(callback.callback());
		transactionContext.setConnection(groupId, connection);
		return connection;
	}

}
